let source = [
  {
    id: 1,
    children: [{ id: 2 }, { id: 3 }],
  },
  {
    id: 4,
    children: [{ id: 5 }, { id: 6 }],
  },
]

var dfs = function (data, id, res, path) {
  for (let k of data) {
    path.push(k)
    if (k.id === id) {
      res.push(path.slice())
      break
    }
    if (k.children) {
      dfs(k.children, id, res, path)
    }
    path.pop()
  }
}

var treeArr = function (data, id) {
  let res = []
  let path = []

  dfs(data, id, res, path)

  return res[0]
}

let r = treeArr(source, 3)

console.log(JSON.stringify(r))
